/*!
 * @文件名 drv_flash.c
 *
 * @功能描述
 *  1.FLASH模块初始化配置
 *  2.FLASH底层读写函数
 */
#include "drv_flash.h"

flash_ssd_config_t flashSSDConfig;
status_t ret;        /* Store the driver APIs return code */
uint32_t failAddr;

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_flash_init
 * Description   : 初始化FLASH模块
 *
 *END**************************************************************************/
void bsp_drv_flash_init(void)
{
    ret = FLASH_DRV_Init(&Flash_InitConfig0, &flashSSDConfig);
    DEV_ASSERT(STATUS_SUCCESS == ret);
}


/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_flash_write
 * Description   : 写FLASH函数，传入参数为buffer和写入地址，写入大小固定为24字节
 *
 *END**************************************************************************/
void bsp_drv_flash_write(uint32_t address,uint8_t * write_buffer,int length)
{
	ret = FLASH_DRV_EraseSector(&flashSSDConfig, address, FEATURE_FLS_DF_BLOCK_SECTOR_SIZE);
	DEV_ASSERT(STATUS_SUCCESS == ret);

	/* Disable Callback */
	flashSSDConfig.CallBack = NULL_CALLBACK;

    /* Verify the erase operation at margin level value of 1, user read */
    ret = FLASH_DRV_VerifySection(&flashSSDConfig, address, length / FTFx_DPHRASE_SIZE, 1u);
    DEV_ASSERT(STATUS_SUCCESS == ret);

    /* Write some data to the erased PFlash sector */
    ret = FLASH_DRV_Program(&flashSSDConfig, address, length, write_buffer);
    DEV_ASSERT(STATUS_SUCCESS == ret);

    /* Verify the program operation at margin level value of 1, user margin */
    ret = FLASH_DRV_ProgramCheck(&flashSSDConfig, address, length, write_buffer, &failAddr, 1u);
    DEV_ASSERT(STATUS_SUCCESS == ret);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_flash_read_byte
 * Description   : 从指定地址读FLASH到buffer
 *
 *END**************************************************************************/
#if 0
uint16_t bsp_drv_flash_read_byte(volatile uint8_t * flashPtr,uint8_t *buffer, uint32_t length)
{
	while (0UL != length)
	{
		*(buffer++) = *(flashPtr++);
		length--;
	}
	return 0;
}
#else
uint16_t bsp_drv_flash_read_byte(volatile uint8_t* flashPtr, uint8_t* buffer, uint32_t length)
{
    // 添加参数检查
    if ((flashPtr == NULL) || (buffer == NULL) || (length == 0U)) {
        return 1; // 返回错误代码
    }

    while (length > 0U)
    {
        // 确保每次读取都是volatile访问
        *buffer = *flashPtr;

        buffer++;
        flashPtr++;
        length--;
    }

    return 0; // 成功返回0
}
#endif
#if 0
/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_get_A_vendor_data
 * Description   : 将FLASH中地址为0x10007C00的数据取出，作为vendor_A的数据
 *
 *END**************************************************************************/
uint16_t bsp_drv_get_A_part_vendor_data(uint8_t *buffer, uint32_t length)
{
    uint32_t u32Addr = 0x10000000;
	u32Addr += SECTOR_ADDR(EFM_SECTOR_VENDOR_A_DATA_NUM);
    __IO uint8_t *pu8Buf = (uint8_t *)u32Addr;
    while (0UL != length) {
       *(buffer++) = *(pu8Buf++);
       length--;
               }
	return 0;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_get_B_part_vendor_data
 * Description   : 将FLASH中地址为0x10007000的数据取出，作为vendor_B的数据
 *
 *END**************************************************************************/
uint16_t bsp_drv_get_B_part_vendor_data(uint8_t *buffer, uint32_t length)
{
    uint32_t u32Addr = 0x10000000;
	u32Addr += SECTOR_ADDR(EFM_SECTOR_VENDOR_B_DATA_NUM);
    __IO uint8_t *pu8Buf = (uint8_t *)u32Addr;
    while (0UL != length) {
       *(buffer++) = *(pu8Buf++);
       length--;
               }
	return 0;
}
#endif

